//
//  LDCHomeViewController.m
//  LaiKeDaoClient
//
//  Created by zouzhisheng on 15/9/25.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "LDCHomeViewController.h"
#import "CustomHomeTableViewCell1.h"
#import "CustomHomeTableViewCell2.h"
#import "CustomTableViewCell3.h"
#import "LKDPatientListModel.h"
#import "KDMypatientTableViewCell.h"
#import "KDPatientAppliListViewController.h"
#import "LDCItemApplyViewController.h"
#import "LDCItemPassViewController.h"
@implementation HomeNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor clearColor];
    LDCHomeViewController *HomeVC=[[LDCHomeViewController alloc] init];
    self.viewControllers = @[HomeVC];
}

@end



@interface LDCHomeViewController (){
    KXJson *mesNumJson;
    LKDPatientListModel *patientModel;
}

@end

@implementation LDCHomeViewController
-(void)viewDidLayoutSubviews
{
    if ([self.displayView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.displayView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.displayView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.displayView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setTabbarHidden:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"首页"];
    [self.view addSubview:self.displayView];
    UIView *headView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headView.backgroundColor=[UIColor colorWithHexString:@"f2f2f2"];
    UIView *lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 9.5, SCREEN_WIDTH, 0.5)];
    lineView.backgroundColor=[UIColor colorWithHexString:@"cccccc"];
    [headView addSubview:lineView];
    self.displayView.tableHeaderView=headView;
    __weak typeof(self)weakSelf = self;
    [CSHApiClient getInfoNumDidFinishLoaded:^(KXJson *json, NSString *path) {
        if ([json getIntForKey:@"code"]==200) {
            mesNumJson=json;
            [weakSelf loadPatientData];
        }
        mesNumJson=json;
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        
    }];
       // Do any additional setup after loading the view.
}
#pragma mark-加载患者数据
-(void)loadPatientData{
     __weak typeof(self)weakSelf = self;
    [CSHApiClient getPatientListKeyword:@"" date:@"" sex:@"" age:@"" disease:@"" solution:@"" didFinishLoaded:^(LKDPatientListModel *model, NSString *path) {
        patientModel=model;
        [weakSelf.displayView reloadData];
    } didFailLoaded:^(NSError *error, NSString *path) {
    }];
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT-64-50)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5+[self filterTodayPatients].count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.row) {
        case 0:
        case 1:
        case 2:
        case 3:
        {
            static NSString *identifier = @"CustomHomeTableViewCell1";
            
            CustomHomeTableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomHomeTableViewCell1"  owner:self options:nil] lastObject];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            [cell loadData:mesNumJson type:indexPath.row];
            return cell;
  
        }
            
            break;
        case 4:
        {
            static NSString *identifier = @"CustomHomeTableViewCell2";
            
            CustomHomeTableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:
                                              identifier];
            if (cell == nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomHomeTableViewCell2"  owner:self options:nil] lastObject];
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
            
 
        }
        
        default:
        {
            
            static NSString* str=  @"cell";
            KDMypatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: str];
            if (cell ==nil) {
                cell = [[KDMypatientTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
                }
            NSArray *patientArray=[self filterTodayPatients];
            [cell reloadData:patientArray[indexPath.row-5]];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;
  
        }
            break;
    }

    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        case 1:
        case 2:
        case 3:
            return 50;
            break;
        case 4:
            return 30;
        default:
            return 55;
            break;
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            KDPatientAppliListViewController *controller = [[KDPatientAppliListViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case 2:
        {
            LDCItemApplyViewController *controller = [[LDCItemApplyViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case 3:
        {
            LDCItemPassViewController *controller = [[LDCItemPassViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];

        }
            break;
        default:
            break;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark-刷选出今日患者
-(NSMutableArray *)filterTodayPatients{
    NSMutableArray *patients=[[NSMutableArray alloc] init];
    if (patientModel.data.count>0) {
        for(LKDPatientListSubModel *subModel in  patientModel.data){
            if ([subModel.group isEqualToString:@"今日患者"]) {
                [patients addObjectsFromArray:subModel.list];
            }
        }
    }
    return patients;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
